package com.tgpms.vforms.controller;

import com.tgpms.auth.security.TokenService;
import com.tgpms.common.Result;
import com.tgpms.exception.*;
import com.tgpms.util.Common;
import com.tgpms.util.PageInfo;
import com.tgpms.vforms.entity.Company;
import com.tgpms.vforms.service.CompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Api(value = "CompanyController", tags = "D4010 单位总清单")
@RestController
@RequestMapping(value = "/ev/vform/company")
public class CompanyController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private CompanyService companyService;

    @Resource
    private TokenService tokenService;
    /**
     *  @Author: Riven on 2020-8-10 15:01
     *  @param:
     *  @return:
     */
    @ApiOperation(value = "查询单位主要信息", notes = "查询单位主要信息", httpMethod = "POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "selparam",value = "查询参数sql",required = false),
    	@ApiImplicitParam(name = "leftdition",value = "左连接sql",required = false),
    	@ApiImplicitParam(name = "condition",value = "查询条件sql",required = false),
    	@ApiImplicitParam(name = "dataowner",value = "数据拥有者",required = false),
    	@ApiImplicitParam(name = "pageNo",value = "页数",required = true,defaultValue = "1"),
    	@ApiImplicitParam(name = "pageSize",value = "每页数量",required = true,defaultValue = "10")
    })
    @PostMapping(value = "/selCompanyList")
    public Result<Object> selCompanyList(@RequestParam(value = "selparam", required = false) String selparam,
                                                 @RequestParam(value = "leftdition", required = false) String leftdition,
                                                 @RequestParam(value = "condition", required = false) String condition,
                                                 @RequestParam(value = "dataowner", required = false) String dataowner,
                                                 @RequestParam(value = "pageNo", required = true) String pageNo,
                                                 @RequestParam(value = "pageSize", required = true) String pageSize) {
        Result<Object> result = new Result<Object>();
        PageInfo pageInfo = new PageInfo();
        if(Common.isEmpty(pageNo) || Common.isEmpty(pageSize)) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
            return result;
        }
        try {
            pageInfo.setPage(pageNo);
            pageInfo.setSize(pageSize);
            Map<String,String> map = new HashMap<String,String>();
            map.put("selparam", selparam);
            map.put("leftdition", leftdition);
            map.put("condition", condition);
            map.put("dataowner", dataowner);
            pageInfo.setQueryMap(map);
            result = companyService.selCompanyList(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
            throw new QueryException(ExceptionExplain.ERROR_BY_QUERY.getExplain());
        }
        return result;
    }

    /**
     *  @Author: Riven on 2020-8-10 15:02
     *  @param: 
     *  @return: 
     */
    @ApiOperation(value = "添加单位总清单接口信息", notes = "添加单位总清单接口信息", httpMethod = "POST")
    @PostMapping(value = "/addCompanyInfo")
    public Result addCompanyInfo(@RequestBody Company company, HttpServletRequest request) {
        Result result = new Result();
        try {
            //获取token操作人
            company.setInsertUser(tokenService.getUserName(request));
            company.setInsertDate(new Date());
            Map<String, String> map = companyService.addCompanyInfo(company);
            if(map.get("code").equals("1")) {
                result.setSuccess(true);
                result.setData(map.get("data"));
            } else {
                result.setSuccess(false);
            }
            result.setMsg(map.get("info"));
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
            throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
        }
        return result;
    }

    /**
     *  @Author: Riven on 2020-8-10 15:02
     *  @param: 
     *  @return: 
     */
    @ApiOperation(value = "更新单位总清单接口信息", notes = "更新单位总清单接口信息", httpMethod = "PUT")
    @PutMapping(value = "/updateCompanyInfo")
    public Result updateCompanyInfo(@RequestBody Company company, HttpServletRequest request) {
        Result result = new Result();
        if(Common.isEmpty(company.getCompany())) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
            return result;
        }
        try {
            //获取token操作人
            company.setModifyUser(tokenService.getUserName(request));
            company.setModifyDate(new Date());
            Map<String, String> map = companyService.updateCompanyInfo(company);
            if (map.get("code").equals("1")) {
                result.setSuccess(true);
            } else {
                result.setSuccess(false);
            }
            result.setMsg(map.get("info"));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
            throw new UpdateException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
        }
        return result;
    }

    /**
     *  @Author: Riven on 2020-8-10 15:02
     *  @param: 
     *  @return: 
     */
    @ApiOperation(value = "删除单位总清单接口信息", notes = "删除单位总清单接口信息", httpMethod = "DELETE")
    @ApiImplicitParam(name = "company",value = "公司编码",required = true)
    @DeleteMapping(value = "/deleteCompanyInfo")
    public Result deleteCompanyInfo(@RequestParam(value = "company", required = true) String company) {
        Result result = new Result();
        if(Common.isEmpty(company)) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
            return result;
        }
        try {
            Map<String, String> map = companyService.deleteCompanyInfo(company);
            if(map.get("code").equals("1")) {
                result.setSuccess(true);
            }else {
                result.setSuccess(false);
            }
            result.setMsg(map.get("info"));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
            throw new DeleteException(ExceptionExplain.ERROR_BY_DELETE.getExplain());
        }
        return result;
    }
}
